package com.magicsoft.app.adapter;

import java.util.List;

import android.content.Context;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.app.entity.ThreeElementsEntity;

public class ThreeElementsAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<ThreeElementsEntity> data;
	private boolean isShowConsult = true;

	public ThreeElementsAdapter(Context c, List<ThreeElementsEntity> data) {
		inflater = LayoutInflater.from(c);
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	public void setShowConsult(boolean isShowConsult) {
		this.isShowConsult = isShowConsult;
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (position == 0 && isShowConsult) {
			convertView = inflater.inflate(R.layout.consult, null);

		} else {
			convertView = inflater.inflate(R.layout.three_elements_item, null);
			
			ImageView img = (ImageView) convertView
					.findViewById(R.id.img_image);
			TextView title = (TextView) convertView
					.findViewById(R.id.tv_title);
			TextView describe = (TextView) convertView
					.findViewById(R.id.tv_describe);
			
			ThreeElementsEntity t = data.get(position);
			
			if (t.getImg() == 0) {
				img.setVisibility(View.GONE);
			} else {
				img.setImageResource(t.getImg());
			}
			title.setText(t.getTitle());
			Object descObj = t.getDescribe();
			if (descObj instanceof String) {
				describe.setText((String) descObj);
			} else if (descObj instanceof Spanned) {
				describe.setText((Spanned) descObj);
			}
		}
		return convertView;
	}
}
